<?php

/**
 * @author AtomskJD
 * @copyright 2012
 */
include_once('RAWhtml-parser-config.inc.php');
$self = $_SERVER['PHP_SELF'];

if (!isset($_COOKIE['curid']) ) {
	setcookie('curid', 'AUD');
	header("Location:". $self);
	exit;
}else{
	$cid = $_COOKIE['curid'];
}
if (!isset($_COOKIE['year']) OR $_COOKIE['year'] == 'all'){
	$year = "";
}else{
	$_year = $_COOKIE['year'];
	$year1 = mktime(0,0,0,1,1,$_year);
	$year2 = mktime(0,0,0,1,1,$_year+1);
	$year = "AND date BETWEEN $year1 AND $year2";
}


 	$var = 'Hello';
	if (isset($_GET['a']) )$var = $_GET['a'];
	$img = imagecreatefrompng('background.png');
	$colors['white'] = imagecolorallocate($img, 255, 255, 255);
	$colors['black'] = imagecolorallocate($img, 0, 0, 0);
	$colors['grey'] = imagecolorallocate($img, 220, 220, 220);
	$colors['grey'] = imagecolorallocate($img, 220, 220, 220);
	$colors['green'] = imagecolorallocate($img, 0, 200, 20);
	$height = 320;
	$width = 750;
	
//	imageline($img, 20, 20, 200, 200, $colors['black']);
	imageFilledRectangle($img,0,0, $width, $height, $colors['grey']);
//	imageTTFtext($img, 30, 10, 300, 150, $colors['black'], "arial.ttf", "Привет!!!");
//	imagestring($img, 3,100,100, $var, $colors['black']);
	$x=0;


$db = mysql_connect(HOST, LOGIN, PASSWORD)
or die (file_put_contents(ERRLOG, mysql_error()."\r\n", FILE_APPEND) );
mysql_select_db(DBNAME, $db)
or die (file_put_contents(ERRLOG, mysql_error()."\r\n", FILE_APPEND) );
$sql = "SELECT date, rate, units, currname FROM currency_rate WHERE cid LIKE '".$cid."'" .$year. " ORDER BY date LIMIT 380";
$result = mysql_query($sql, $db)
or die (file_put_contents(ERRLOG, mysql_error()."\r\n", FILE_APPEND) );
while($row = mysql_fetch_array($result, MYSQL_ASSOC) ){
    $query[] = $row;
}

if (!empty($query[0])){
	imagettftext($img, 20, 0, 10, 40, $colors['black'], 'tahoma.ttf', $query[0]['currname']);
	$decrise = 0;
	foreach($query as $array){
		$calcMAX[] = $array['rate'] / $array['units'];
	}
	
	$MAX = $height / max($calcMAX);
//		var_dump($calcMAX);
	foreach($query as $array){
		$y = round(($array['rate'] / $array['units'])*$MAX-200);
		$ext = date("d/m",$array['date'])."  ".$y."<br>";
		imagefilledrectangle($img, $x, 320, $x+0, 320-$y, $colors['green']);
		$x+=2;
	}
}
mysql_free_result($result);
mysql_close($db);


	header('Content-type: Image/png');
	imagepng($img);
?>